//
//  MyGoldController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/7.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "MyGoldController.h"
#import "MyScoreCell.h"
#import "WalletDetailController.h"

@interface MyGoldController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *incomeBtn;
@property (weak, nonatomic) IBOutlet UIButton *exchangeBtn;
@property (weak, nonatomic) IBOutlet UILabel *goldNumLabel;
@property (nonatomic , strong) SegementScrollView * segementView;
@property (nonatomic , strong) NSMutableDictionary * dataDict;
@property (nonatomic , strong) UITableView * tableview;

@property (nonatomic , strong) NSMutableArray * dataArray;
@end

@implementation MyGoldController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)setNavi
{
    self.title = @"我的金币";
    [self.rightBtn setImage:Image(@"icon_nav_说明") forState:(UIControlStateNormal)];
}

-(void)setUI
{
    self.incomeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    self.exchangeBtn.layer.masksToBounds = YES;
    self.exchangeBtn.layer.cornerRadius = 15;
    self.view.backgroundColor = Color(245, 245, 245);
    self.segementView = [[SegementScrollView alloc]initWithFrame:CGRectMake(0, 155, kScreenWidth, 40) andtitleArray:@[@"全部",@"收入",@"支出"]];
    [self.view addSubview:self.segementView];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.segementView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.segementView.frame)) style:(UITableViewStylePlain)];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.tableFooterView = [UIView new];
    [self.tableview registerNib:[UINib nibWithNibName:@"MyScoreCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    __weak typeof(self) weakSelf = self;
    self.segementView.selectedBlock = ^(NSInteger index) {
        
        if (index == 0) {
            [weakSelf.dataDict setValue:@"all" forKey:@"type"];
            
        }else if (index == 1)
        {
            [weakSelf.dataDict setValue:@"in" forKey:@"type"];
        }else if (index == 2)
        {
            [weakSelf.dataDict setValue:@"out" forKey:@"type"];
        }
        [weakSelf getDataWithDict:weakSelf.dataDict];
    };
}


- (void)getDataWithDict:(NSDictionary *)dict
{
    [self.dataArray removeAllObjects];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/gold-integration-lists",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            for (int i = 0; i < [[responseObject objectForKey:@"data"] count]; i ++) {
                GoldScoreDetailModel * model = [[GoldScoreDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"][i]];
                [self.dataArray addObject:model];
            }
            [self.tableview reloadData];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)setData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/my-gold-integration",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [self.incomeBtn setTitle:[NSString stringWithFormat:@"今日收益:%@",[responseObject objectForKey:@"data"][@"today_gold_format"]] forState:(UIControlStateNormal)];
            self.goldNumLabel.text = [responseObject objectForKey:@"data"][@"integration_gold_account_format"];
            [self getDataWithDict:self.dataDict];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyScoreCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell bindData:self.dataArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WalletDetailController * vc = [[WalletDetailController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSMutableDictionary *)dataDict
{
    if (!_dataDict) {
        _dataDict = [NSMutableDictionary dictionary];
        [_dataDict setValue:self.userToken forKey:@"token"];   
        [_dataDict setValue:@"all" forKey:@"type"];
        [_dataDict setValue:@"1" forKey:@"page"];
    }
    return _dataDict;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)rightBtnAction:(UIButton *)sender
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
